package work.sby.web;


import work.sby.pojo.Buy;
import work.sby.pojo.User;
import work.sby.service.BuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllByUserName")
public class SelectAllByUserName extends HttpServlet {
    private BuyService service =new BuyService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        // 从 session 中获取名为 "user" 的属性
        User user = (User) session.getAttribute("user");
        //1.调用Brandservice
        //2.根据用户名查询购买记录
        String username = user.getUsername();
        List<Buy> buys = service.selectByBuyName(username);
        //2.将list存入request域中
        request.setAttribute("buys",buys);
        //3.转发到页面
        request.getRequestDispatcher("/buy.jsp").forward(request,response);

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}